package com.xhwl.centiotdata.handler.pedestrian.impl;

import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationFactory;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationHandler;
import com.xhwl.centiotdata.pojo.constant.ZhongKongConstant;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.zhongkong.PedestrianRecordZhongKongDTO;
import com.xhwl.centiotdata.service.cent.IPedestrianCentService;
import com.xhwl.centiotdata.suppliersystem.zhongkong.PedestrianZhongKongHandler;
import com.xhwl.centiotdata.utils.UploadUtils;
import com.xhwl.common.dto.cent.pedes.OpenDoorRecordDTO;
import com.xhwl.common.enums.AccessApproachEnum;
import com.xhwl.common.enums.AccessTypeEnum;
import com.xhwl.common.enums.DirectionEnum;
import com.xhwl.common.enums.SourceTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/06/ 11:31
 */
@Service
public class PedestrianIntegrationZhongKongHandler extends PedestrianIntegrationHandler {

    @Autowired
    private PedestrianZhongKongHandler pedestrianZhongKongHandler;
    @Autowired
    private IPedestrianCentService pedestrianCentService;
    @Autowired
    private UploadUtils uploadUtils;

    @Override
    public void afterPropertiesSet() throws Exception {
        PedestrianIntegrationFactory.register(13, this);
    }

    @Override
    public void pullRecord(PedestrianIntegrationConfigDTO configDTO, Integer intervalDays, Date date) {
        // 拉取数据
        List<PedestrianRecordZhongKongDTO> recordZhongKongDTOList = pedestrianZhongKongHandler.pullRecord(configDTO);
        if (CollectionUtils.isEmpty(recordZhongKongDTOList)) {
            return;
        }
        // 转换数据
        List<OpenDoorRecordDTO> openDoorRecordDTOList = this.handleRecord(configDTO, recordZhongKongDTOList);
        // 存储业务中台
        pedestrianCentService.saveBatch(openDoorRecordDTOList);
    }

    private List<OpenDoorRecordDTO> handleRecord(PedestrianIntegrationConfigDTO configDTO, List<PedestrianRecordZhongKongDTO> recordZhongKongDTOList) {
        return recordZhongKongDTOList.stream()
                .filter(recordZhongKongDTO -> ZhongKongConstant.ACCESS_EVENT_SET.contains(recordZhongKongDTO.getEventName()))
                .map(recordZhongKongDTO -> {
                    return OpenDoorRecordDTO.builder()
                            .projectId(configDTO.getProjectId())
                            .code(recordZhongKongDTO.getId())
                            .openUserId(recordZhongKongDTO.getPin())
                            .userName(recordZhongKongDTO.getName())
                            .openDeviceId(recordZhongKongDTO.getDevSn())
                            .deviceName(recordZhongKongDTO.getDevName())
                            .arrivalTime(recordZhongKongDTO.getEventTime())
                            .arrivalAddress(recordZhongKongDTO.getAccZone())
                            .pictureUrl(this.handlePictureUrl(recordZhongKongDTO.getPhotoBase64()))
                            .sourceType(this.handleSourceType(recordZhongKongDTO.getPin()))
                            .direction(this.handleDirection(recordZhongKongDTO.getReaderName()))
                            .type(this.handleType(recordZhongKongDTO.getVerifyModeName(), recordZhongKongDTO.getEventName()))
                            .state((short) 1)
                            .productId(13)
                            .supplierId(21)
                            .approach(AccessApproachEnum.OFF_LINE.getId()).build();
                }).collect(Collectors.toList());
    }

    private Short handleDirection(String readerName) {
        if (StringUtils.isEmpty(readerName)) {
            return DirectionEnum.UNKNOWN.getId();
        }
        if (readerName.contains("入")) {
            return DirectionEnum.IN.getId();
        }
        if (readerName.contains("出")) {
            return DirectionEnum.OUT.getId();
        }
        return DirectionEnum.UNKNOWN.getId();
    }

    private String handlePictureUrl(String photoBase64) {
        if (StringUtils.isEmpty(photoBase64)) {
            return null;
        }
        // 截断 data:image/jpg;base64,
        return uploadUtils.uploadImgBase64(photoBase64.substring(22));
    }

    private Integer handleSourceType(String pin) {
        if (StringUtils.isEmpty(pin)) {
            return SourceTypeEnum.USER.type;
        }
        if (pin.startsWith("8")) {
            return SourceTypeEnum.VISITOR_USER.type;
        } else {
            return SourceTypeEnum.USER.type;
        }
    }

    private Short handleType(String verifyModeName, String eventName) {
        if (StringUtils.isEmpty(verifyModeName)) {
            return AccessTypeEnum.OTHER.getId();
        }
        switch (verifyModeName) {
            case ZhongKongConstant.ACCESS_MODE_CARD:
                return AccessTypeEnum.IC_CARD.getId();
            case ZhongKongConstant.ACCESS_MODE_FACE:
                return AccessTypeEnum.FACE.getId();
            case ZhongKongConstant.ACCESS_MODE_FINGERPRINT:
                return AccessTypeEnum.FINGERPRINT.getId();
            case ZhongKongConstant.ACCESS_MODE_PASS_WORD:
                return AccessTypeEnum.PASS_WORD.getId();
            case ZhongKongConstant.ACCESS_MODE_OTHER:
                switch (eventName) {
                    case ZhongKongConstant.ACCESS_EVENT_REMOTELY_OPEN:
                    case ZhongKongConstant.ACCESS_EVENT_REMOTE_NORMALLY_OPEN:
                        return AccessTypeEnum.REMOTE.getId();
                    case ZhongKongConstant.ACCESS_EVENT_BUTTON_OUT:
                        return AccessTypeEnum.BUTTON.getId();
                    case ZhongKongConstant.ACCESS_EVENT_EMERGENCY_PASS_WORD:
                        return AccessTypeEnum.PASS_WORD.getId();
                }
            default:
                return AccessTypeEnum.OTHER.getId();
        }
    }

}
